package com.rongzhe.house.presenter;

import android.content.Intent;
import android.view.View;

import com.baidu.location.BDLocation;
import com.rongzhe.house.R;
import com.rongzhe.house.entity.HouseCriteriaTo;
import com.rongzhe.house.entity.voo.Banner;
import com.rongzhe.house.entity.voo.City;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.BannerManager;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.manager.PositionManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.fragment.FragmentMain;
import com.rongzhe.house.utils.UIUtils;

import java.util.List;

/**
 * Created by huang on 2017/11/22.
 */

public class MainPagePresenter extends BasePresenter {

    public MainPagePresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void onCreate() {
        BannerManager.getInstance().getBanners(PositionManager.getInstance().getCurrentCityId(),
                new DataListener<List<Banner>>() {
                    @Override
                    public void onSuccess(List<Banner> data, String message) {
                        ((FragmentMain) getUiControlInterface()).setBannerImage(data);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {

                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    int[] mCheckPosition = new int[5];

    private void onQueryComplate(final List<HouseOverview> data, String type) {
        if (getString(R.string.rent_together).equals(type)) {
            mCheckPosition[0] = 1;
            if (data != null) {
                ((FragmentMain) getUiControlInterface()).setRentShareList(data);
            }
        } else if (getString(R.string.rent_all).equals(type)) {
            mCheckPosition[1] = 1;
            if (data != null) {
                ((FragmentMain) getUiControlInterface()).setRentAllList(data);
            }
        } else if (getString(R.string.rent_direct).equals(type)) {
            mCheckPosition[2] = 1;
            if (data != null) {
                ((FragmentMain) getUiControlInterface()).setRentDirectList(data);
            }
        } else if (getString(R.string.rent_recomment).equals(type)) {
            mCheckPosition[3] = 1;
            if (data != null) {
                ((FragmentMain) getUiControlInterface()).setRentRecommend(data);
            }
        } else if (getString(R.string.rent_apartment).equals(type)) {
            mCheckPosition[4] = 1;
            if (data != null) {
                ((FragmentMain) getUiControlInterface()).setRentApartmentList(data);
            }
        }
        boolean loadFinish = true;
        for (int i : mCheckPosition) {
            if (i == 0) {
                loadFinish = false;
                break;
            }
        }
        if (loadFinish) {
            getUiControlInterface().dismissProgress();
            ((FragmentMain) getUiControlInterface()).updateUi();
        }
    }

    private void loadData(final String type) {
        HouseCriteriaTo houseCriteriaTo = new HouseCriteriaTo();
        if (getString(R.string.rent_together).equals(type)) {
            houseCriteriaTo.setAsEntireRent(false);
        } else if (getString(R.string.rent_all).equals(type)) {
            houseCriteriaTo.setAsEntireRent(true);
        } else if (getString(R.string.rent_direct).equals(type)) {
            houseCriteriaTo.setAsOwnerRent(true);
        } else if (getString(R.string.rent_apartment).equals(type)) {
            houseCriteriaTo.setAsApartMent(true);
        }
        HouseManager.getInstance().queryHouse(5, 1,
                new DataListener<List<HouseOverview>>() {
                    @Override
                    public void onSuccess(List<HouseOverview> data, String message) {
                        onQueryComplate(data, type);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        getUiControlInterface().showToast(message);
                        onQueryComplate(null, type);
                    }

                    @Override
                    public void onEnd() {
                    }
                }, houseCriteriaTo);
    }

    private void loadRecommandData() {
        HouseManager.getInstance().getFirstPageHouse(
                PositionManager.getInstance().getCurrentCityId(),
                new DataListener<List<HouseOverview>>() {
                    @Override
                    public void onSuccess(List<HouseOverview> data, String message) {
                        onQueryComplate(data, getString(R.string.rent_recomment));
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        getUiControlInterface().showAlert(UIUtils.createHintMessageWithClick(mContext,
                                mContext.getString(R.string.hint),
                                mContext.getString(R.string.message_network_error),
                                mContext.getString(android.R.string.ok), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getUiControlInterface().finish();
                                    }
                                }));
                        getUiControlInterface().dismissProgress();
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    public void loadAllData() {
        getUiControlInterface().showProgress(mContext.getString(R.string.loading));
        loadRecommandData();
        loadData(getString(R.string.rent_together));
        loadData(getString(R.string.rent_all));
        loadData(getString(R.string.rent_direct));
        loadData(getString(R.string.rent_apartment));
    }

    private PositionManager.BDLocationChangedListener mLocationListener
            = new PositionManager.BDLocationChangedListener() {
        @Override
        public void onBDLocationChanged() {
            BDLocation bdlocation = PositionManager.getInstance().getBDLocation();
            if (bdlocation == null) {
                return;
            }
            String cityName = bdlocation.getCity();
            City city = PositionManager.getInstance().getCityFromName(cityName);
            if (city == null) {
                getUiControlInterface().showAlert(UIUtils.createHintDialog(mContext, getString(R.string.hint),
                        getString(R.string.you_city_not_in_service), null));
            } else {
                PositionManager.getInstance().setCurrentCity(city);
                loadAllData();
            }
        }
    };

    @Override
    public void start(Intent intent) {
        super.start(intent);
        loadAllData();
        PositionManager.getInstance().addBDLocationChangedListenr(mLocationListener);
    }

    @Override
    public void stop() {
        super.stop();
        PositionManager.getInstance().removeBDLocationChangedListenr(mLocationListener);
    }
}
